<%-- 
    Document   : index
    Created on : 25 Apr 12, 13:16:00
    Author     : Reynald
--%>
<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>

               
                <%  
                    //session.getAttribute("username");
                    String driver = "org.gjt.mm.mysql.Driver";
                    Class.forName(driver).newInstance();

                    Connection con=null;
                    ResultSet rst=null;
                    Statement stmt=null;

                    try{
                    String url="jdbc:mysql://localhost:3306/progin_171_13509008?user=root";
                    con=DriverManager.getConnection(url);
                    stmt=con.createStatement();
                    }
                    catch(Exception e){
                    System.out.println(e.getMessage());
                    }

                    String username = request.getParameter("username");
                    String password = request.getParameter("password");
                    String email = request.getParameter("email");
                    String birthdate = request.getParameter("birthdate");
                    String about = request.getParameter("about_me");
                    String gender = request.getParameter("gender");
                    String pathFile = (String)(session.getAttribute("pathImage"));
                    boolean pass = false;

                    //cek username
                    boolean checkUsername = true;
                    String queryCheckUsername = "SELECT * FROM user where user_name='"+username+"'";
                    rst = stmt.executeQuery(queryCheckUsername);
                    rst.last();
                    int rowcheckUserName = rst.getRow();
                    out.println(username);
                    rst.first();
                    if(rowcheckUserName == 0){
                        checkUsername = true;
                    }else{
                        checkUsername = false;
                    }
                    
                    //cek email
                    boolean checkEmail = true;
                    String queryCheckEmail = "SELECT * FROM user where email='"+email+"'";
                    rst = stmt.executeQuery(queryCheckEmail);
                    rst.last();
                    int rowcheckEmail = rst.getRow();
                    rst.first();
                    if(rowcheckEmail == 0){
                        checkEmail = true;
                    }else{
                        checkEmail = false;
                    }
                    
                    //cek tanggal
                    boolean checkDate = true;
                    String[] dateSeparated = new String[3];
                    if(birthdate != null){
                        dateSeparated = birthdate.split("-");
                        if(dateSeparated[1] == "2"){
                            if((Integer.parseInt(dateSeparated[0])%4 == 0 && Integer.parseInt(dateSeparated[0])%100 != 0 )|| Integer.parseInt(dateSeparated[0])%400 == 0){
                                //cek tahun kabisat
                                if(Integer.parseInt(dateSeparated[2]) > 0 && Integer.parseInt(dateSeparated[2]) <= 29){
                                    checkDate = true;
                                }else{
                                    checkDate = false;
                                }
                            }else{
                                if(Integer.parseInt(dateSeparated[2]) > 0 && Integer.parseInt(dateSeparated[2]) <= 28){
                                    checkDate = true;
                                }else{
                                    checkDate = false;
                                }
                            }
                        }else{
                            if((Integer.parseInt(dateSeparated[1])%2 != 0 && Integer.parseInt(dateSeparated[1]) > 0 && Integer.parseInt(dateSeparated[1]) <= 7)||(Integer.parseInt(dateSeparated[1])%2 == 0 && Integer.parseInt(dateSeparated[1]) > 7 && Integer.parseInt(dateSeparated[1]) <=12)){
                                //cek apakah bulan tersebut Januari, Maret, Mei, Juli, Agustus, Oktober, Desember
                                if(Integer.parseInt(dateSeparated[2]) > 0 && Integer.parseInt(dateSeparated[2]) <= 31){
                                    checkDate = true;
                                }else{
                                    checkDate = false;
                                }
                            }else{
                                if((Integer.parseInt(dateSeparated[1])%2 == 0 && Integer.parseInt(dateSeparated[1]) > 0 && Integer.parseInt(dateSeparated[1]) <= 7)||(Integer.parseInt(dateSeparated[1])%2 != 0 && Integer.parseInt(dateSeparated[1]) > 7 && Integer.parseInt(dateSeparated[1]) <=12)){
                                    //cek apakah bulan tersebut April, Juni, September, November
                                    if(Integer.parseInt(dateSeparated[2]) > 0 && Integer.parseInt(dateSeparated[2]) <= 30){
                                        checkDate = true;
                                    }else{
                                        checkDate = false;
                                    }
                                }else{
                                    checkDate = false;
                                }
                            }
                        }
                    }
                    if(checkUsername == true && checkEmail == true && checkDate == true){
                        pass = true;
                        String queryInsertUser = "INSERT INTO user VALUES ('"+username+"','"+password+"','"+email+"','"+about+"','"+gender+"','"+pathFile+"','"+birthdate+"')";
                        //int row = stmt.executeUpdate(queryInsertUser);
                        try{ 
                            int i;
                            i = stmt.executeUpdate(queryInsertUser);
                        } catch (SQLException e){
                            out.println("Caught SQL Exception: " + e);
                        }
                       
                    }
                    
                    
                    if(!checkUsername){
                        out.println("Username already exists");
                    }
                    if(!checkEmail){
                        out.println("Email was already used");
                    }
                    
                    if (pass == true){
                        response.sendRedirect("profile.jsp");
                        session.setAttribute("user_name", username);
                    }
                    
                %>

